import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import '../style/wx_style.dart';

///
/// TabBar控件，重新设定脚手架，增加TabBar与PageView的切换
///
class CustomScaffoldWidget extends StatefulWidget {
  final bool resizeToAvoidBottomPadding; //用于Scaffold中的body是否随键盘展示而缩放，true表示是
  final TabBarPosition position;
  final List<Widget>? tabItems;
  final List<Widget>? tabViews;

  final Color? backgroundColor;

  final Color? indicatorColor;

  final Widget? title;
  final Widget? drawer;
  final Widget? floatingActionButton;
  final FloatingActionButtonLocation?
      floatingActionButtonLocation; //悬浮按钮，flutter 官方demo中中间那个按钮就是这个属性实现
  final Widget?
      bottomTabBar; //当position="top"时 也就是此控件需要顶部tabBar时，需要再设定一个底部的tabBar

  final List<Widget>?
      footerButtons; //底部常驻按钮部件，与bottomNavigationBar是有区别的，不具备导航能力。

  final ValueChanged<int>?
      onPageChanged; //ValueChanged 是回调函数，别名：void Function(T value);
  final ValueChanged<int>? onDoublePress;
  final ValueChanged<int>? onSinglePress;

  CustomScaffoldWidget(
      {super.key,
      this.position = TabBarPosition.top,
      this.resizeToAvoidBottomPadding = true,
      this.backgroundColor,
      this.indicatorColor,
      this.tabItems,
      this.tabViews,
      this.title,
      this.drawer,
      this.bottomTabBar,
      this.floatingActionButton,
      this.floatingActionButtonLocation,
      this.footerButtons,
      this.onDoublePress,
      this.onPageChanged,
      this.onSinglePress});

  @override
  State<StatefulWidget> createState() => CustomTabBarState();
}

class CustomTabBarState<StatefulWidget> extends State<
        CustomScaffoldWidget> // SingleTickerProviderStateMixin 配合动画使用，定时刷新动画帧
    with
        SingleTickerProviderStateMixin {
  PageController? _pageController;
  TabController? _tabController;

  int _index = 0;

  _navigationPageChanged(index) {
    if (_index == index) {
      return;
    }
    _index = index;

    _tabController!.animateTo(index);
    widget.onPageChanged?.call(index);
  }

  _navigationTapClick(index) {
    if (_index == index) {
      return;
    }
    _index = index;
    widget.onPageChanged?.call(index);

    //不想要动画
    _pageController!.jumpTo(MediaQuery.sizeOf(context).width * index);
    widget.onSinglePress?.call(index);
  }

  @override
  void initState() {
    super.initState();
    _pageController = PageController();
    this._tabController =
        TabController(length: widget.tabItems!.length, vsync: this);
  }

  @override
  void dispose() {
    this._tabController!.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.position == TabBarPosition.top) {
      return Scaffold(
        drawer: widget.drawer,
        backgroundColor: WxColors.mainBackgroundColor,
        resizeToAvoidBottomInset: widget.resizeToAvoidBottomPadding,
        floatingActionButton: SafeArea(
          // ?? 等同于kotlin的 ?:
          child: widget.floatingActionButton ?? Container(),
        ),
        floatingActionButtonLocation: widget.floatingActionButtonLocation,
        persistentFooterButtons: widget.footerButtons,
        appBar: AppBar(
          backgroundColor: Theme.of(context).primaryColor,
          title: widget.title,
          bottom: TabBar(
            labelColor: Colors.amber,
            controller: _tabController,
            tabs: widget.tabItems!,
            indicatorColor: widget.indicatorColor,
            onTap: _navigationTapClick,
          ),
        ),
        body: PageView(
          controller: _pageController,
          onPageChanged: _navigationPageChanged,
          children: widget.tabViews!,
        ),
        bottomNavigationBar: widget.bottomTabBar,
      );
    } else {
      return Scaffold(
          drawer: widget.drawer,
          backgroundColor: WxColors.mainBackgroundColor,
          appBar: AppBar(
            backgroundColor: Theme.of(context).primaryColor,
            title: widget.title,
          ),
          body: PageView(
            controller: _pageController,
            onPageChanged: _navigationPageChanged,
            children: widget.tabViews!,
          ),
          bottomNavigationBar: Material(
            color: Theme.of(context).primaryColor,
            child: SafeArea(
              child: TabBar(
                labelColor: Colors.amber,
                controller: _tabController,
                tabs: widget.tabItems!,
                indicatorColor: widget.indicatorColor,
                onTap: _navigationTapClick,
              ), //TODO:自定义TabBar，设置手势GestureDetector，增加点击滑动到顶部处理
            ),
          ));
    }
  }
}

enum TabBarPosition { top, bottom }
